#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the version 3 of the GNU Lesser General Public License
#   as published by the Free Software Foundation.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU Lesser General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Copyright (c) NEC Deutschland GmbH, NEC HPC Europe
#
# $Id$

from unittest import TestCase, TestSuite

from basic_types.marshaller.marshaller_test_types import *


__all__ = ["MarshallerTestCase", "MarshallerTestSuite"]


class MarshallerTestCase(TestCase):
    def __init__( self, obj ):
        TestCase.__init__( self )
        self.obj = obj

    def createMarshaller( self ): raise NotImplementedError
    def createUnmarshaller( self, marshaller ): return None 

    def runTest( self ):
        marshaller = self.createMarshaller()
        marshalled_obj = self.obj.marshal( marshaller )
        unmarshaller = self.createUnmarshaller( marshaller )        
        if unmarshaller is not None:
            unmarshalled_obj = type( self.obj )()
            unmarshalled_obj.unmarshal( unmarshaller )
            if unmarshalled_obj != self.obj and repr( unmarshalled_obj ) != repr( self.obj ):
                print self.__class__.__name__ + ": got", repr(unmarshalled_obj), " and expected", repr(self.obj)
                self.fail()

    def shortDescription( self ):
        return self.__class__.__name__ + "(" + self.obj.__class__.__name__ + ")"


class MarshallerTestSuite(TestSuite):
    def __init__( self, marshaller_test_case_type ):
        TestSuite.__init__( self )
        self.addTest( marshaller_test_case_type( BooleanStruct( True ) ) )
        self.addTest( marshaller_test_case_type( BooleanStruct( False ) ) )
        self.addTest( marshaller_test_case_type( EmptyStruct() ) )    
        self.addTest( marshaller_test_case_type( FloatStruct( -1.0 ) ) )
        self.addTest( marshaller_test_case_type( FloatStruct( 0.0 ) ) )
        self.addTest( marshaller_test_case_type( FloatStruct( 1.0 ) ) )
        self.addTest( marshaller_test_case_type( DoubleStruct( -1.0 ) ) )
        self.addTest( marshaller_test_case_type( DoubleStruct( 0.0 ) ) )
        self.addTest( marshaller_test_case_type( DoubleStruct( 1.0 ) ) )
        self.addTest( marshaller_test_case_type( Int32Struct( -1 ) ) )
        self.addTest( marshaller_test_case_type( Int32Struct( 0 ) ) )
        self.addTest( marshaller_test_case_type( Int32Struct( 1 ) ) )
        self.addTest( marshaller_test_case_type( Int64Struct( -1 ) ) )
        self.addTest( marshaller_test_case_type( Int64Struct( 0 ) ) )
        self.addTest( marshaller_test_case_type( Int64Struct( 1 ) ) )
        self.addTest( marshaller_test_case_type( StringStruct( "" ) ) )
        self.addTest( marshaller_test_case_type( StringStruct( "test" ) ) )
        self.addTest( marshaller_test_case_type( StringSetStruct( "a", "b" ) ) )
        
